<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar left-icon="arrowleft" @clickLeft="back" title="我要代理填写" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="mywantagentapply">
			<view class="apply-list">
				<view class="apply-list-span">手机号</view>
				<view class="apply-list-input"><input type="text" v-model="tel" placeholder="请输入手机号" placeholder-style="color:#CCCCCC" /></view>
				<view @click="onClickSendCode" class="apply-list-yzm">{{ getMessageText }}</view>
			</view>
			<view class="apply-list-one">
				<view class="apply-list-one-span">验证码</view>
				<view class="apply-list-one-input">
					<input type="text" v-model="code" placeholder="请输入验证码" placeholder-style="color:#CCCCCC" />
					<view @click="code = ''" class="apply-list-one-input-close">×</view>
				</view>
			</view>
			<view class="apply-list-one">
				<view class="apply-list-one-span" style="display: flex;justify-content: space-between;">
					<em>姓</em>
					<em>名</em>
				</view>
				<view class="apply-list-one-input">
					<input type="text" v-model="name" placeholder="请输入姓名" placeholder-style="color:#CCCCCC" />
					<view @click="name = ''" class="apply-list-one-input-close">×</view>
				</view>
			</view>
			<view class="apply-list-picker">
				<view class="apply-list-picker-span">代理区域</view>
				<view v-if="label" @click="onClickOpenAddress" class="apply-list-picker-input">{{ label }}</view>
				<view v-else @click="onClickOpenAddress" class="apply-list-picker-input apply-list-picker-input-h">请选择地址</view>
			</view>
			<view @click="onClickSubmitForm" class="apply-btn" :class="{ 'apply-btn-active': tel && code && name && provinceId && cityId && areaId }">提交申请</view>
			<!-- 鬼也晓得我为什么放在外面，但是如果放在里面的话，会有BUG -->
			<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirmAddress"
			 themeColor="#007AFF"></simple-address>
		</view>
	</view>
</template>

<script>
	import {
		config
	} from '@/utils/config.js';
	import {
		myMixin
	} from '@/utils/mixin.js';

	import simpleAddress from '@/components/simple-address/simple-address.nvue';
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
	export default {
		mixins: [myMixin],
		components: {
			uniNavBar
		},
		data() {
			return {
				tel: '',
				code: '',
				name: '',
				timer: null,
				countdown: 60,
				getMessageText: '发送验证码',
				cityPickerValueDefault: [0, 0, 0],
				label: '',
				provinceId: '',
				cityId: '',
				areaId: '',
				province: '',
				city: '',
				area: ''
			};
		},
		components: {
			simpleAddress
		},
		// 页面卸载的时候，一定要清除定时器
		onUnload() {
			if (this.timer) {
				clearTimeout(this.timer);
			}
		},
		methods: {
			back() {
				uni.navigateBack({})
			},
			// 倒计时操作
			beginSetTime() {
				if (this.countdown === 0) {
					this.getMessageText = '发送验证码';
					this.countdown = 60;
					this.timer = null;
					return;
				} else {
					this.getMessageText = `${this.countdown}s后重发`;
					this.countdown--;
				}
				this.timer = setTimeout(() => {
					this.beginSetTime();
				}, 1000);
			},
			onClickOpenAddress() {
				this.$refs.simpleAddress.open();
			},
			onConfirmAddress(e) {
				this.label = e.label;
				this.provinceId = e.provinceCode;
				this.cityId = e.cityCode;
				this.areaId = e.areaCode;
				this.province = e.provinceName;
				this.city = e.cityName;
				this.area = e.areaName;
			},
			onClickSubmitForm() {
				const token = uni.getStorageSync('token');
				if (!token) {
					uni.showToast({
						icon: 'none',
						title: '暂无登录凭证, 无法提交申请'
					});
					return false;
				}
				if (!this.name) {
					uni.showToast({
						icon: 'none',
						title: '请填写姓名'
					});
					return false;
				}
				if (!this.tel) {
					uni.showToast({
						icon: 'none',
						title: '请填写电话'
					});
					return false;
				}
				if (!this.code) {
					uni.showToast({
						icon: 'none',
						title: '请填写验证码'
					});
					return false;
				}
				if (!this.provinceId) {
					uni.showToast({
						icon: 'none',
						title: '请选择省市区'
					});
					return false;
				}
				if (!this.cityId) {
					uni.showToast({
						icon: 'none',
						title: '请选择省市区'
					});
					return false;
				}
				if (!this.areaId) {
					uni.showToast({
						icon: 'none',
						title: '请选择省市区'
					});
					return false;
				}
				uni.showLoading({ title: '加载中' })
				uni.request({
					url: `${config.api_base_url}member/agent_apply/${this.code}`,
					method: 'POST',
					data: {
						checkCode: this.code,
						contact: this.name,
						phone: this.tel,
						provinceId: this.provinceId,
						province: this.province,
						cityId: this.cityId,
						city: this.city,
						areaId: this.areaId,
						area: this.area
					},
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('token'),
						'Content-Type': 'application/json'
					},
					success: ((res) => {
						let { status, data } = res.data;
						if (status.flag === true) {
							uni.showToast({
								icon: 'none',
								title: '保存成功'
							});
							setTimeout(function() {
								uni.switchTab({
									url: '../my/my'
								})
							}, 2000)
						} else {
							uni.showToast({
								icon: 'none',
								title: status.msg
							});
						}
					}),
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '无法申请, 网络异常'
						});
					},
					complete: (() => {
						uni.hideLoading()
					})
				});
			},
			onClickSendCode() {
				if (this.timer) {
					return false;
				}
				if (!this.tel) {
					uni.showToast({
						icon: 'none',
						title: '请填写您的手机号'
					});
				}
				if (!/^1[345678]\d{9}$/.test(this.tel)) {
					uni.showToast({
						title: '手机号码格式错误',
						icon: 'none'
					});
					return false;
				}
				this.beginSetTime();
				uni.request({
					url: `${config.api_base_url_tools}sms`,
					method: 'POST',
					data: {
						sender: 'member',
						flag: 'agentApply',
						phone: this.tel
					},
					header: {
						'Content-Type': 'application/json'
					},
					success: res => {
						let {
							status,
							data
						} = res.data;
						if (status.flag === true) {
							uni.showToast({
								icon: 'none',
								title: '发送成功'
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: status.msg
							});
						}
					},
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '发送失败, 网络异常'
						});
					}
				});
			}
		}
	};
</script>
<style>
	page {
		background: #f5f5f5;
	}
</style>
<style scoped>
	.mywantagentapply {
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 4rpx 12rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 12rpx;
		box-sizing: border-box;
		padding: 45rpx;
	}

	.apply-list {
		overflow: hidden;
		margin-bottom: 20rpx;
	}

	.apply-list-span {
		font-size: 30rpx;
		color: #333;
		height: 76rpx;
		line-height: 76rpx;
		margin-right: 24rpx;
		flex-shrink: 0;
		float: left;
		margin-right: 24rpx;
		font-weight: 500;
		width: 90rpx;
	}

	.apply-list-input {
		width: 372rpx;
		height: 72rpx;
		background: rgba(255, 255, 255, 1);
		border-radius: 12rpx;
		border: 2rpx solid rgba(221, 221, 221, 1);
		float: left;
	}

	.apply-list-input input {
		width: 100%;
		padding: 0rpx 20rpx;
		box-sizing: border-box;
		font-size: 30rpx;
		color: #333;
		line-height: 72rpx;
		height: 72rpx;
	}

	.apply-list-yzm {
		color: #1677ff;
		font-size: 28rpx;
		float: right;
		line-height: 76rpx;
	}

	.apply-list-one {
		/* overflow: hidden; */
		margin-bottom: 20rpx;
		display: flex;
	}

	.apply-list-one-span {
		font-size: 30rpx;
		color: #333;
		height: 76rpx;
		line-height: 76rpx;
		margin-right: 24rpx;
		flex-shrink: 0;
		float: left;
		margin-right: 24rpx;
		font-weight: 500;
		width: 90rpx;
	}

	.apply-list-one-span em {
		font-style: initial;
	}

	.apply-list-one-input {
		width: 100%;
		height: 80rpx;
		border-radius: 12rpx;
		border: 2rpx solid rgb(221, 221, 221);
		display: flex;
		align-items: center;
		box-sizing: border-box;
	}

	.apply-list-one-input input {
		padding: 0;
		margin: 0;
		outline: none;
		border: none;
		width: calc(100% - 60rpx);
		height: 100%;
		box-sizing: border-box;
		padding-left: 22rpx;
		font-size: 30rpx;
		line-height: 80rpx;
	}

	.apply-list-one-input-close {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		background-color: #c6c6c6;
		line-height: 30rpx;
		text-align: center;
		color: #fff;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.apply-list-picker {
		/* overflow: hidden; */
		margin-bottom: 20rpx;
		display: flex;
	}

	.apply-list-picker-span {
		font-size: 30rpx;
		color: #333;
		height: 76rpx;
		line-height: 76rpx;
		margin-right: 24rpx;
		flex-shrink: 0;
		float: left;
		margin-right: 24rpx;
		font-weight: 500;
	}

	.apply-list-picker-input {
		width: 100%;
		height: 80rpx;
		border-radius: 12rpx;
		border: 2rpx solid rgb(221, 221, 221);
		display: flex;
		align-items: center;
		box-sizing: border-box;
		font-size: 30rpx;
		line-height: 80rpx;
		padding-left: 11rpx;
	}

	.apply-list-picker-input-h {
		color: #ccc;
	}

	.apply-list-picker-input uni-picker {
		padding: 0;
		margin: 0;
		outline: none;
		border: none;
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding-left: 22rpx;
		font-size: 30rpx;
		line-height: 80rpx;
		position: relative;
	}

	.apply-list-picker-input uni-picker::after {
		content: '';
		position: absolute;
		right: 30rpx;
		top: 50%;
		width: 20rpx;
		height: 20rpx;
		border-left: 5rpx solid #c6c6c6;
		border-top: 5rpx solid #c6c6c6;
		transform: translate3d(0, -50%, 0) rotateZ(225deg);
		-webkit-transform: translate3d(0, -50%, 0) rotateZ(225deg);
		margin-top: -4rpx;
	}

	.apply-btn {
		width: 660rpx;
		height: 82rpx;
		background: rgba(240, 34, 48, 1);
		border-radius: 14rpx;
		color: #fff;
		font-size: 32rpx;
		line-height: 82rpx;
		text-align: center;
		position: fixed;
		bottom: 80rpx;
		left: 50%;
		margin-left: -330rpx;
		display: none;
	}

	.apply-btn-active {
		display: block;
	}
</style>
